/**
 * @filename:SysPermissionServiceImpl 2025-05-27
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.sys.permission.service.impl;

import com.starhub.common.bean.BrowserBean;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.generator.genid.IdGenerator;
import com.starhub.engine.sys.permission.entity.SysPermission;
import com.starhub.engine.sys.permission.dao.SysPermissionDao;
import com.starhub.engine.sys.permission.service.SysPermissionService;
import com.starhub.engine.sys.role.dao.SysRolePermissionDao;
import com.starhub.engine.sys.role.entity.SysRolePermission;
import com.starhub.utils.ColorUtil;
import com.starhub.utils.Util;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

/**   
 * @Description:(权限表服务实现)
 *
 * @version: V1.0
 * @author: luosw
 * 
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Autowired
    private SysPermissionDao permissionDao;

    @Autowired
    private SysRolePermissionDao rolePermissionDao;

    /**
    * 新增
    * @param user
    * @param permission
    * @return
    */
    public Long add(LoginUser user, SysPermission permission){

        Long id = IdGenerator.generateId();

        //数据封装，默认值添加
        permission.setId(id);
        permission.setCreateTime(new Date());
        permission.setUpdateTime(new Date());
        permission.setDeleteType(0);

        permissionDao.insertSelective(permission);
        return id;
    }

    /**
    * 更新
    * @param user
    * @param permission
    * @return
    */
    public SysPermission update(LoginUser user, SysPermission permission){
        permission.setUpdateTime(new Date());
        //不允许更新的属性设置为null
        permissionDao.updateByPrimaryKeySelective(permission);
        return permission;
    }

    /**
    * 基本信息获取
    * @param user
    * @param id
    * @return
    */
    public SysPermission getInfo(LoginUser user, Long id){

        SysPermission permission = permissionDao.selectByPrimaryKey(id);
        SysRolePermission sysRolePermission = new SysRolePermission();
        sysRolePermission.setPermissionId(id);
        List<SysRolePermission> sysRolePermissions = rolePermissionDao.selectByPrimaryKeySelective(new SysRolePermission());
        if(CollectionUtils.isNotEmpty(sysRolePermissions)){
            List<Long> roleIds = sysRolePermissions.stream().map(SysRolePermission::getRoleId).collect(Collectors.toList());
            Set<String> roleIdList = roleIds.stream().map(e -> Util.null2String(e)).collect(Collectors.toSet());
            permission.setRoles(roleIdList);
        }
        return permission;
    }

    /**
    * (批量)删除，同时支持单条或者批量,此方法为逻辑删除
    * @param user
    * @param ids
    */
    public void deleteByType(LoginUser user, String ids,int deleteType){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
            //删除状态，目前只有1的逻辑
        permissionDao.deleteSysPermissionByType(idList, deleteType);
    }

    /**
    * 根据id删除，此方法为真实删除
    * @param user
    * @param ids
    */
    public void deleteRecord(LoginUser user, String ids){

        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        permissionDao.deleteSysPermissionRecord(idList);
    }

    /**
    * 列表查询
    * @param user
    * @param params
    */
    public List<SysPermission> getList(LoginUser user,  Map<String , Object>  params){

        List<SysPermission> permissionList =  new ArrayList<SysPermission>();
            //TODO
        int pageSize = 10;
        int current = 1;
        SysPermission record = new SysPermission();
        record.setDeleteType(0);
        permissionList = permissionDao.selectByPrimaryKeySelective(record);

        if(CollectionUtils.isNotEmpty(permissionList)){
            List<SysRolePermission> sysRolePermissions = rolePermissionDao.selectByPrimaryKeySelective(new SysRolePermission());
            if(CollectionUtils.isNotEmpty(sysRolePermissions)){
                Map<String,Set<String>> rolePermission = new HashMap<>();
                for(SysRolePermission permission:sysRolePermissions){
                    String permissionId = Util.null2String(permission.getPermissionId());
                    String roleId = Util.null2String(permission.getRoleId());
                    if(rolePermission.containsKey(permissionId)){
                        Set<String> roleIds = rolePermission.get(permissionId);
                        roleIds.add(roleId);
                        rolePermission.put(permissionId,roleIds);
                    }else{
                        Set<String> roleIds = new HashSet<>();
                        roleIds.add(roleId);
                        rolePermission.put(permissionId,roleIds);
                    }
                }
                for(SysPermission sysPermission:permissionList){
                    sysPermission.setRoles(rolePermission.get(Util.null2String(sysPermission.getId())));
                }
            }
        }
        return permissionList;
    }

    @Override
    public Map<String, BrowserBean> getActivePermission(LoginUser user, Map<String, Object> params) {

        Map<String, BrowserBean> result = new HashMap<>();
        SysPermission record = new SysPermission();
        record.setDeleteType(0);
        List<SysPermission> permissionList = permissionDao.selectByPrimaryKeySelective(record);
        // 查询所有有效权限
        List<String> dColors = ColorUtil.dColors;
        int i =0 ;
        if(CollectionUtils.isNotEmpty(permissionList)){
            for(SysPermission sysPermission : permissionList){
                int co = i % dColors.size();
                i++;
                String color = dColors.get(co);
                BrowserBean bean = new BrowserBean(Util.null2String(sysPermission.getId()) ,sysPermission.getName(),color);
                result.put(Util.null2String(sysPermission.getId()),bean);
            }
        }
        return result;
    }

}
